package concurency.vehicletracker.immutable;

import java.util.*;
import java.util.concurrent.*;

public class DelegatingVehicleTracker {
	private final ConcurrentMap<String, ImmutablePoint> locations;
	private final Map<String, ImmutablePoint> unmodifiableLocations;

	public DelegatingVehicleTracker(Map<String, ImmutablePoint> data) {
		locations = new ConcurrentHashMap<String, ImmutablePoint>(data);
		unmodifiableLocations = Collections.unmodifiableMap(locations);
	}

	public ImmutablePoint getLocation(String name) {
		ImmutablePoint loc = locations.get(name);
		if (loc == null)
			throw new IllegalArgumentException("No such ID: " + name);
		return loc;
	}

	public void setLocation(String name, ImmutablePoint point) {
		if (locations.replace(name, point) == null) {
			throw new IllegalArgumentException("No such ID: " + name);
		}
	}

	public Map<String, ImmutablePoint> getLocations() {
		return unmodifiableLocations;
	}

}
